<?php
namespace app\admin\controller;
use think\Db;
use app\admin\controller\Auth;
use think\Config;
class Demo extends Auth{
	// 案例分类列表
	public function demoType(){
		$list = Db::name('demotype')->paginate(10);
		$this->assign('list',$list);
		return $this->fetch();
	}
	// 渲染添加案列分类
	public function addDemoType(){
		return $this->fetch();
	}
	// 执行添加案例分类
	public function doAddDemoType(){
		$name = trim(input('post.name'));
		if(empty($name)){
			$this->error('名称不能为空');
			exit;
		}
		$data['name'] = trim($name);
		$data['createtime'] = time();
		$result = Db::name('demotype')->insert($data);
		if($result){
			$this->redirect('/admin/demo/addDemoType');
		}else{
			$this->error('添加失败');
		}
	}
	// 删除案例分类
	public function delDemoType(){
		$id = trim(input('did'));
		if(empty($id)){
			$this->error('禁止非法操作');
			exit;
		}
		$where['id'] = array('EQ',$id);
		$result = Db::name('demotype')->where($where)->delete();
		if($result){
			$this->redirect('/admin/demo/demoType');
		}else{
			$this->error('删除失败');
		}
	}
	// 添加案例
	public function addDemo(){
		$typelist = Db::name('demotype')->select();
		$this->assign('list',$typelist);
		return $this->fetch();
	}
	// 执行添加案例
	public function doAddDemo(){
		$arr = ['title','introduce','demotype','content'];
		$info_param = input('post.');
		$check_res = $this->check_param($arr,$info_param);
		if(!$check_res){
			$this->error('请将表单补充完整');
			exit;
		}
		// 获取表单上传文件 例如上传了001.jpg
		$file = request()->file('image');
		// 移动到框架应用根目录/public/uploads/ 目录下
		if(empty($file)){
			$this->error('请选择图片');
			exit;
		}
		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
		if($info){
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			$info_param['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
		}else{
			// 上传失败获取错误信息
			$this->error($file->getError());
			exit;
		}
		// 获取表单上传文件 例如上传了001.jpg
		$classic_file = request()->file('classic_image');
		// 移动到框架应用根目录/public/uploads/ 目录下
		if(!empty($classic_file)){
			$info = $classic_file->move(ROOT_PATH . 'public' . DS . 'uploads');
			if($info){
				// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
				$info_param['classic_logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
			}else{
				// 上传失败获取错误信息
				$this->error($classic_file->getError());
				exit;
			}
		}
		$info_param['createtime'] = time();
		$result = Db::name('demo')->insert($info_param);
		if($result){
			$this->redirect('/admin/demo/addDemo');
		}else{
			$this->error('添加失败');
		}
	}
	// 案例列表
	public function demoList(){
		$list = Db::name('demo')->paginate(10,false,['query' => request()->param()]);
		$this->assign('list',$list);
		return $this->fetch();
	}
	// 删除案例
	public function delDemo(){
		$did = input('did');
		if(empty($did)){
			$this->error('禁止非法操作');
			exit;
		}
		$where['id'] = array('EQ',$did);
		$result = Db::name('demo')->where($where)->delete();
		if($result){
			$this->redirect('/admin/demo/demoList');
		}else{
			$this->error('删除失败');
		}
	}
	// 案例详情
	public function demoInfo(){
		$did = input('did');
		if(empty($did)){
			$this->error('禁止非法操作');
			exit;
		}
		$where['id'] = array('EQ',$did);
		$info = Db::name('demo')->where($where)->find();
		if(empty($info)){
			$this->error('该案例不存在');
			exit;
		}
		$list = Db::name('demotype')->select();
		$this->assign('list',$list);
		$this->assign('info',$info);
		return $this->fetch();
	}
	// 修改案例
	public function updateDemo(){
		$did = input('post.did');
		if(empty($did)){
			$this->error('出现未知错误，请退出刷新重试');
			exit;
		}
		$title = trim(input('post.title'));
		if(!empty($title)){
			$data['title'] = $title;
		}else{
			$this->error('名称不能为空');
			exit;
		}
		$introduce = trim(input('post.introduce'));
		if(!empty($introduce)){
			$data['introduce'] = $introduce;
		}else{
			$this->error('介绍不能为空');
			exit;
		}
		$demotype = trim(input('post.demotype'));
		if(!empty($demotype)){
			$data['demotype'] = $demotype;
		}else{
			$this->error('分类不能为空');
			exit;
		}
		$content = trim(input('post.content'));
		if(!empty($content)){
			$data['content'] = $content;
		}else{
			$this->error('详情不能为空');
			exit;
		}
		// 获取表单上传文件 例如上传了001.jpg
		$file = request()->file('image');
		// 移动到框架应用根目录/public/uploads/ 目录下
		if(!empty($file)){
			$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
			if($info){
				// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
				$info_param['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
			}else{
				// 上传失败获取错误信息
				$this->error($file->getError());
				exit;
			}
		}
		$result = Db::name('demo')->where('id = ' . $did)->update($data);
		if($result){
			$this->redirect('/admin/demo/demoList');
		}else{
			$this->error('修改失败');
		}
	}
	// 案例logo
	public function demoLogo(){
		$logo = Db::name('navigation_logo')->where('type = 2')->value('logo');
		$this->assign('logo',$logo);
		return $this->fetch();
	}
	// 修改案例logo
	public function updateDemoLogo(){
		$image = request()->file('image');
		if(empty($image)){
			$this->error('请选择图片');
			exit;
		}
		$info = $image->move(ROOT_PATH . 'public' . DS . 'uploads');
		if($info){
			// 成功上传后 获取上传信息
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			$data['logo'] = Config::get('view_replace_str.__UPLOAD__').'/'.$info->getSaveName();
		}else{
			$this->error($image->getError());
			exit;
		}
		$where['type'] = array('EQ',2);
		$data['createtime'] = time();
		$result = Db::name('navigation_logo')->where($where)->update($data);
		if($result){
			$this->redirect('/admin/demo/demoLogo');
		}else{
			$this->error('修改失败');
		}
	}
}